package com.oamicnet.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/file")
public class FileUploadController {

    @GetMapping("/index")
    public String index(Model model) {
        return "file/list";
    }

    @RequestMapping("fileUpload")
    public String fileUpload(@RequestParam("img") MultipartFile img, Model model) throws IOException {
        // 判断图片是否为空
        if (img != null) {
            // 设置图片路径
            String oldname = img.getOriginalFilename();
            String filepath = "D:\\"+oldname;
            // 转存图片
            img.transferTo(new File(filepath));
            model.addAttribute("imgName",oldname+"，上传成功!");
        }
        return "file/list";
    }
}
